package org.thirdHero.admin.web;

import com.github.pagehelper.PageInfo;
import org.thirdHero.admin.annotation.LoginAdmin;
import org.thirdHero.core.util.ResponseUtil;
import org.thirdHero.core.util.StringUtil;
import org.thirdHero.db.domain.SysRole;
import org.thirdHero.db.domain.SysUser;
import org.thirdHero.db.service.SysRoleService;
import org.thirdHero.db.vo.SysRoleVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin/role")
public class RoleController {

    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    SysRoleService sysRoleService;

    @GetMapping("/list")
    public Object list(@LoginAdmin SysUser sysUser , String name,
                       @RequestParam(value = "page",defaultValue = "1")Integer page,
                       @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                       String sort, String order){
        List<SysRoleVo> litemallRoles = sysRoleService.querySelective(name, page, limit, sort, order);
        PageInfo<SysRoleVo> pageInfo = new PageInfo<>(litemallRoles);
        ResponseUtil<SysRoleVo> responseUtil = new ResponseUtil<>();
        return  responseUtil.ok("成功",pageInfo.getTotal(),litemallRoles);
    }
    @PostMapping("/create")
    @ResponseBody
    public Object addRole(@LoginAdmin SysUser sysUser ,
                          @RequestBody SysRole role){
        SysRole sysRole = this.sysRoleService.queryRoleByName(role.getName());
        if(sysRole !=null){
            return ResponseUtil.errorBussiness("该角色已存在");
        }
        int result = this.sysRoleService.saveRole(role);
        if(result != 1){
            return ResponseUtil.fail(-1,"报错失败");
        }else{
            return ResponseUtil.ok("保存成功");
        }
    }
    @PutMapping("/editRole/{id}")
    @ResponseBody
    public Object editRole(@LoginAdmin SysUser sysUser, @RequestBody SysRole sysRole, @PathVariable String id){
        SysRole dbSysRole = this.sysRoleService.queryRoleById(Integer.parseInt(id));
        if(dbSysRole !=null){
            if(!sysRole.getName().equals(dbSysRole.getName())){
                SysRole sameRoleName = this.sysRoleService.queryRoleByName(sysRole.getName());
                if(sameRoleName!=null){
                    return ResponseUtil.fail(-1,"该部门信息已存在");
                }
            }
            sysRole.setId(dbSysRole.getId());
            int result = this.sysRoleService.updateRole(sysRole);
            if(result == 1){
                return ResponseUtil.ok("更新成功");
            }else{
                return ResponseUtil.fail(-1,"更新失败请重试");
            }
        }else{
            return ResponseUtil.fail(-1,"没有找到该角色信息");
        }
    }
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Object deleteRole(@LoginAdmin SysUser sysUser ,@PathVariable String id){
        // willAdd:这里应该坚持该角色是否有绑定的用户，如果有，那么将不允许删除
        int result = this.sysRoleService.deleteRoleById(Integer.parseInt(id));
        if(result == 1){
            return ResponseUtil.ok("删除成功");
        }else{
            return ResponseUtil.fail(-1,"删除失败请重试");
        }
    }
}
